<?php

namespace app\admin\controller;

use app\user\model\UserModel;
use app\admin\model\MailDowloadModel;
use cmf\controller\AdminBaseController;

class MailController extends AdminBaseController
{
    public function add()
    {
        if ($this->request->isPost()) {
            if (! $mailboxName = $this->request->param('mailbox_name')) {
                $this->error('请输入必填项');
            }
            if (! $mailboxPassword = $this->request->param('mailbox_password')) {
                $this->error('请输入必填项');
            }
            if (! $mailboxType = $this->request->param('mailbox_type')) {
                $this->error('请输入必填项');
            }

            $adminId = (new UserModel())->get(cmf_get_current_admin_id())['id'];

            $condition = [
                'admin_id' => $adminId,
                'mailbox'  => $mailboxName
            ];

            if (! MailDowloadModel::get($condition)) {
                (new MailDowloadModel())->add($adminId, $mailboxName, $mailboxType, $mailboxPassword);
            }

            $this->redirect(url('Mail/mailboxList'), 0);
        }
        return $this->fetch();
    }

    public function mailboxList()
    {
        $mailbox = (new MailDowloadModel)->where(['admin_id' => (new UserModel())->get(cmf_get_current_admin_id())['id']])->order('id', 'desc')->select();
        $this->assign('mailbox', $mailbox);
        return $this->fetch();
    }

    public function dowload()
    {
        $mailboxId = $this->request->get('id');

        if ($mailbox = (new MailDowloadModel)->where(['id' => $mailboxId])->find()) {
            pclose(popen('sh -e /data/pythonproject/maildown.sh ' . $mailbox['mailbox'] .
                ' ' . $mailbox['mailbox_password'] . ' ' . $mailbox['mailbox_type'] . ' &', 'r'));
        }
        $this->success('下载中，请稍后查看');
    }

    public function maillist()
    {
        $mailboxId = $this->request->get('id');
        $condition = [
            'id'       => $mailboxId,
            'admin_id' => (new UserModel())->get(cmf_get_current_admin_id())['id']
        ];
        if (! $mailbox = MailDowloadModel::get($condition)) {
            $this->error('这不是你的邮箱');
        }
        try {
            $dir = scandir('/data/html/website/public/mail/' . $mailbox['mailbox']);
        } catch (\Exception $e) {
            $this->error('暂无邮件，稍后重试！');
        }

        if ($dir[0] == '.' || $dir[0] == '..') {
            unset($dir[0]);
        }

        if ($dir[1] == '.' || $dir[1] == '..') {
            unset($dir[1]);
        }
        rsort($dir);
        $this->assign('dir', $dir);

        $this->assign('mailbox', $mailbox['mailbox']);
        return $this->fetch();
    }

    public function update()
    {
        if ($this->request->isPost()) {
            $mailboxId = $this->request->param('id');
            if (! $mailboxName = $this->request->param('mailbox_name')) {
                $this->error('请输入必填项');
            }
            if (! $mailboxPassword = $this->request->param('mailbox_password')) {
                $this->error('请输入必填项');
            }
            if (! $mailboxType = $this->request->param('mailbox_type')) {
                $this->error('请输入必填项');
            }
            if ($mailbox = (new MailDowloadModel)->where(['id' => $mailboxId])->find()) {
                $mailbox->data([
                    'mailbox'          => $mailboxName,
                    'mailbox_password' => $mailboxPassword,
                    'mailbox_type'     => $mailboxType
                ]);
                $mailbox->save();
            }
            $this->redirect(url('Mail/mailboxList'), 0);
        }
        $mailboxId = $this->request->get('id');
        $mailbox = (new MailDowloadModel)->where(['id' => $mailboxId])->find();
        $this->assign('mailbox', $mailbox);
        return $this->fetch();
    }

    public function delete()
    {
        $mailboxId = $this->request->get('id');
        $mailbox = (new MailDowloadModel)->where(['id' => $mailboxId])->find();
        //exec('rm -rf /data/html/website/public/mail/' . $mailbox['mailbox']);
        $mailbox->delete();
        $this->redirect(url('Mail/mailboxList'), 0);
    }
}
